package com.airlines.base.config.datasource;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * @author xxxx
 * @date 2018年3月15日
 * @description 主数据源配置
 */
@Configuration
@MapperScan(basePackages = { PrimaryDataSourceConfig.BASE_PACKAGE, PrimaryDataSourceConfig.BASE_PACKAGE2,
		PrimaryDataSourceConfig.BASE_PACKAGE3, PrimaryDataSourceConfig.BASE_PACKAGE4,
		PrimaryDataSourceConfig.BASE_PACKAGE5 }, sqlSessionTemplateRef = PrimaryDataSourceConfig.DATA_SOURCE_NAME
				+ "SqlSessionTemplate")
public class PrimaryDataSourceConfig {

	public PrimaryDataSourceConfig() {
		System.out.println("ini primary data source...");
	}

	// 如果新增或者修改数据源，更新配置以下3个常量即可
	public static final String DATA_SOURCE_NAME = "foc";
	public static final String TRANSACTION_MANAGER = DATA_SOURCE_NAME + "TransactionManager";
	public static final String BASE_PACKAGE = "com.airlines.base.dao";
	public static final String BASE_PACKAGE2 = "com.airlines.crewassignment.dao";
	public static final String MAPPER_PACKAGE = "classpath*:mybatis/mapper/**/*.xml";
	public static final String BASE_PACKAGE3 = "com.airlines.runlimit.dao";
	public static final String BASE_PACKAGE4 = "com.airlines.system.dao";
	public static final String BASE_PACKAGE5 = "com.airlines.basedata.dao";

	/**
	 * @Primary 只有在主数据源才需要加，次数据源不能加@Primary
	 *
	 */
	@Bean(name = DATA_SOURCE_NAME + "DataSource")
	@ConfigurationProperties(prefix = "spring.datasource." + DATA_SOURCE_NAME)
	@Primary
	public DataSource dataSource() {
		return DataSourceBuilder.create().build();
	}

	@Bean(name = DATA_SOURCE_NAME + "SqlSessionFactory")
	@Primary
	public SqlSessionFactory sqlSessionFactory(@Qualifier(DATA_SOURCE_NAME + "DataSource") DataSource dataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

		bean.setDataSource(dataSource);
		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_PACKAGE));
		return bean.getObject();
	}

	@Bean(name = TRANSACTION_MANAGER)
	@Primary
	public DataSourceTransactionManager transactionManager(
			@Qualifier(DATA_SOURCE_NAME + "DataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

	@Bean(name = DATA_SOURCE_NAME + "SqlSessionTemplate")
	@Primary
	public SqlSessionTemplate sqlSessionTemplate(
			@Qualifier(DATA_SOURCE_NAME + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}
